package net.huashitong.synthetical.newSupervision.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.synthetical.newSupervision.entity.TaskMessage;
import net.huashitong.synthetical.newSupervision.filter.TaskMessageFilter;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/16
 * Time: 18:50
 * To change this template use File | Settings | File Templates.
 * Description:
 */

@Repository
public class TaskMessageDao extends StringDao<TaskMessage> {

    /**
     * 条件查询
     *
     * @param filter
     * @return
     */
    public List<TaskMessage> queryFilter(TaskMessageFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getTaskId())) {
            query.append(" and taskId=:taskId").setParam("taskId", filter.getTaskId());
        }
        if (StringUtils.isNotBlank(filter.getTaskName())) {
            query.append(" and taskName like:taskName").setParam("taskName", StringUtils.getLikewords(filter.getTaskName()));
        }
        if (StringUtils.isNotBlank(filter.getSendDeptName())) {
            query.append(" and sendDeptName like:sendDeptName").setParam("sendDeptName", StringUtils.getLikewords(filter.getSendDeptName()));
        }
        if (StringUtils.isNotBlank(filter.getContent())) {
            query.append(" and content like :content").setParam("content", StringUtils.getLikewords(filter.getContent()));
        }
        if (StringUtils.isNotBlank(filter.getState())) {
            query.append(" and state = :state ").setParam("state", filter.getState());
        }
        if (StringUtils.isNotBlank(filter.getType())) {
            query.append(" and type=:type").setParam("type", filter.getType());
        }
        if (StringUtils.isNotBlank(filter.getAccepterId())) {
            query.append(" and accepterId =:accepterId").setParam("accepterId", filter.getAccepterId());
        }

        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("sysId");
                filter.getPaging().setSortVal("desc");
            }
        }
        return query.listResult(filter);
    }


    /**
     * 根据taskId批量删除
     *
     * @param taskIds
     */
    public void deleteByTaskIds(List<String> taskIds) {
        StringBuffer hql = (new StringBuffer("delete from TaskMessage")).append(" obj where obj.taskId in(:taskIds)");
        Query query = this.getSession().createQuery(hql.toString());
        query.setParameterList("taskIds", taskIds);
        query.executeUpdate();
    }
}
